<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\im\service;


use app\im\model\ImMessage;

class ImMessageService
{

    /**
     * @param $uniacid
     * @param $userId
     * @param $friendId
     * @功能说明: 我和好友之间聊天的最后一条记录
     * @author jingshuixian
     * @DataTime: 2020/1/16 16:57
     */
    public static function  getLastMessage($uniacid , $userId , $friendId ){

        $whereOr1[] = ['user_id' ,'=' , $friendId] ;
        $whereOr1[] = ['target_id' ,'=' ,$userId  ] ;

        $whereOr2[] = ['user_id' ,'=' ,$userId ] ;
        $whereOr2[] = ['target_id' ,'=' ,$friendId ] ;

        $imMessage = new ImMessage() ;

        $message = $imMessage->where([['uniacid', '=', $uniacid] , ['status', 'in', [1,2] ]])->whereOr(function ($query) use ($whereOr1) {
            $query->where($whereOr1);
        })->whereOr(function ($query) use ($whereOr2) {
            $query->where($whereOr2);
        })->order('id desc')->find();

        return $message ;

    }
}